package middleware.llvm.instruction;

import middleware.llvm.value.IRType;
import middleware.llvm.value.Value;

import java.io.PrintStream;

/**
 * 零扩展指令，用于将整数扩展到更大的位宽
 *
 * @author 杨凯伟
 * @date 2024/10/30
 */
public class ZExtInstruction extends Instruction {
    private final IRType destType;
    private Value value;

    public void setValue(Value value) {
        this.value = value;
    }

    public Value getValue() {
        return value;
    }

    public ZExtInstruction(IRType destType, Value value) {
        super(destType, value);
        this.destType = destType;
        this.value = value;
    }

    @Override
    public void replaceOperand(int pos, Value newOperand) {
        super.replaceOperand(pos, newOperand);
        if (pos == 0) {
            value = newOperand;
        }
    }

    @Override
    public void dump(PrintStream out) {
        out.printf("  %s = zext %s to %s\n", getName(), value.toString(), destType);
    }
}
